package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.CheckGroup;
import com.itcast.pojo.CheckItem;
import com.itcast.pojo.Setmeal;
import com.itcast.service.CheckgroupService;
import com.itcast.service.SetmealService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查组管理
 */

@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Reference
    private CheckgroupService checkgroupService;

    @Reference
    private SetmealService setmealService;

    /**
     * 新增检查组需要的检查项数据
     * @return  返回检查项数据，和是否成功消息
     */
    @RequestMapping("/findCheckitemsAll")
    public Result checkitemsfindAll(){
        try {
            List<CheckItem> checkItemList = checkgroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 新增检查组
     * @param checkGroup  传递的表单数据
     * @param checkitemIds  传递的与检查组关联的检查项ID
     * @return  返回失败成功消息
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){
        try {
            checkgroupService.add(checkGroup,checkitemIds);
            setmealService.generaStaticSetmealDetailHtml(setmealService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
              return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkgroupService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            CheckGroup checkGroup = checkgroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    /**
     * 查询检查组所关联的检查项
     * @param id
     * @return
     */

    @RequestMapping("/findCheckitemIds")
    public Result findCheckitemIds (int id){
        try {
            List<Integer> checkitemIdsByid = checkgroupService.findCheckitemIdsByid(id);
            return  new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkitemIdsByid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 设置检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkgroupService.edit(checkGroup,checkitemIds);
            List<Setmeal> setmealList = checkgroupService.findSetmealList(checkGroup.getId());
            if (setmealList!=null&&setmealList.size()>0) {
                setmealService.generaStaticSetmealDetailHtml(setmealList);
            }
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findAll")
    public Result checkGroupfindAll(){
        try {
            List<CheckGroup> checkGroups = checkgroupService.findcheckgroupAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }



}
